function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return (r[2]);
    return null; //返回参数值
}

var orderNum = getUrlParam("orderNum");
var gateway = getUrlParam("gateway");
var payerId = getUrlParam("payerId");
var goodsName = null;
var bindId = null;
var accessBindId = null;
var orderIp = null;
var phone = null;
var rememberCard = null;
var name = null;
var terminalId = 1212121221313;
var terminalType = "IMEI";
var time = 0;
var timer = null;

// 确定订单信息
mui(document.body).on('tap', '.confirm-btn', function (e) {
    var btn = mui(this);
    var params = {};
    if (orderIp != sessionStorage.getItem("ipAddress")) {
        mui.alert("支付环境异常");
        btn.button('reset');
        return;
    }
    if (accessBindId == "" || accessBindId == null) {
        mui.alert("请选择银行卡");
        return false;
    }
    if (bindId == "" || bindId == null || bindId == 'null') {
        var params = {
            "orderNum": orderNum,
            "gateway": gateway,
            "payerId": payerId,
            "orderIp": orderIp,
            "terminalType": terminalType,
            "terminalId": terminalId,
            "accessBindId": accessBindId
        };
        btn.button('loading');
        $.ajax({
            url: "/thmlfApi/avs/hlbquick/createpaynotbind" + "?_d=" + Date.parse(new Date()),
            type: "POST",
            data: JSON.stringify(params),
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            success: function (r) {
                btn.button('reset');
                if (r.code == 200) {
                    mui('#forward').popover('toggle');
                    $("#bank_num_row").css("display", "block");
                    $("#amount").val(r.result.amount + "元");
                    $("#order_no").val(r.result.orderNum);
                    $("#bank_num_li").val(r.result.cardNum);
                } else {
                    mui.alert(r.message);
                }

            },
            error: function (e) {
                btn.button('reset');
                mui.alert("支付系统繁忙");
            }
        });
    } else {
        mui('#forward').popover('toggle');
        $("#amount").val($("#money").val() + '元');
        $("#order_no").val(orderNum);
        $("#bank_num_row").css("display", "none");
    }
});

// 获取短信验证码
mui(document.body).on('tap', '.code-btn', function (e) {
    if (time > 0) {
        return;
    }
    var that = mui(this);
    that.button('loading');
    if (bindId === null || bindId === '' || bindId === undefined || bindId === 'null') {
        $.get("/thmlfApi/avs/hlbquick/sendpaycode?phone=" + phone + "&gateway=" + gateway + "&orderNum=" + orderNum + "&_d=" + Date.parse(new Date()), function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                time = 60;
                timer = setInterval(times, 1000);
            }
        });
    } else {
        $.get("/thmlfApi/avs/hlbquick/sendbindpaycode?phone=" + phone + "&gateway=" + gateway + "&orderNum=" + orderNum + "&bindId=" + bindId + "&_d=" + Date.parse(new Date()), function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                time = 60;
                timer = setInterval(times, 1000);
            }
        });
    }
});

function times() {
    if (time > 0) {
        $("#sms_code_btn").text(time + 's');
        time--;
    } else {
        clearInterval(timer);
        time = 0;
        $("#sms_code_btn").text('获取');
    }
}

// 确认支付
mui(document.body).on('tap', '.mui-btn-danger', function (e) {
    //	window.location.href = 'pay-success.html';
    var that = mui(this);
    that.button('loading');
    var validateCode = $("#sms_code_input").val().trim();
    if (validateCode == "" || validateCode == null) {
        mui.alert("短信验证码不能为空");
        that.button('reset');
        return false;
    }
    var params = {
        "validateCode": validateCode,
        "orderNum": orderNum,
        "orderIp": orderIp,
        "gateway": gateway,
        "rememberCard": $("#rememberCard").prop("checked") ? 1 : 0,
        "phone": phone,
        "holdName": name,
        "bindId": bindId,
        "accessBindId": accessBindId,
        "terminalType": terminalType,
        "terminalId": terminalId
    };
    $.ajax({
        url: "/thmlfApi/avs/hlbquick/confirmpayms" + "?_d=" + Date.parse(new Date()),
        type: "POST",
        data: JSON.stringify(params),
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (r) {
            that.button('reset');
            if (r.code != 200) {
                mui.alert(r.message);
            } else {
                mui.alert("支付成功.");
                window.location.href = "pay-success.html"
            }
        },
        error: function (e) {
            mui.alert("网络错误，请稍后再试");
            that.button('reset');
        }
    });
});

$(function () {
    $.ajax({
        type: 'GET',
        url: "/thmlfApi/avs/pay/getpayerbind?payerId=" + payerId + "&gateway=hlbquickpay" + "&orderNum=" + orderNum + "&_d=" + Date.parse(new Date()),
        success: function (result) {
            if (result.code == 200) {
                $("#money").val(result.result.order.amountTotal);
                goodsName = result.result.order.remark;
                orderIp = result.result.order.orderIp;
            }
            if (result.code == 200 && result.result.payerBind.length == 0) {
                mui.toast("请先添加银行卡");
            }
            document.getElementById("select-card").style.display = "block";
            if (result.code == 200 && result.result.payerBind.length > 0) {
                var payerBindList = result.result.payerBind;
                for (var i = 0; i < payerBindList.length; i++) {
                    var cardType = payerBindList[i].cardType == "X" ? "信用卡" : "借记卡";
                    var item = '<div class="mui-input-row mui-radio mui-left bind-card-item" data-access-bind-id="' + payerBindList[i].accessBindId + '" data-bindid="' + payerBindList[i].bindId + '" data-phone="' + payerBindList[i].phone + '">' +
                        '<label>' + payerBindList[i].cardName + '&nbsp;&nbsp;' + cardType + '&nbsp;&nbsp;' + payerBindList[i].holdName + '&nbsp;&nbsp;' + '尾号' + payerBindList[i].lastFourNum + '</label>' +
                        '<input name="card" type="radio">' +
                        '</div>';
                    $(".add-new-card").before(item);
                }
                bindChoose();
            }
        }
    });
    $("#addBindCard").click(function () {
        bindId = null;
        window.location.href = "card-manage.html";
        // document.getElementById("select-card").style.display = "none";
        // document.getElementById("add-card").style.display = "block";
    });
});

// 选择银行卡
function bindChoose() {
    $(".bind-card-item").click(function () {
        bindId = $(this).attr("data-bindid");
        phone = $(this).attr("data-phone");
        accessBindId = $(this).attr("data-access-bind-id");
    })
}